var calculator = {

    input: null,
    output: null,
    warnig: null,
    loading: null,

    wsdl: '',
    expression: '',

    timer: null,

    ready: function(){

        this.input = $(this.input);
        this.output = $(this.output);
        this.warnig = $(this.warnig);
        this.loading = $(this.loading);

        this.wsdl =  $(this.wsdl).attr('href');


        this.input.keyup(function() {

            $this = calculator;

            if($this.value()){

                if($this.timer != null)
                    clearTimeout($this.timer);

                $this.timer = setTimeout($this.request, 1000);
            }

        });

    },

    value: function(){
        $val = this.input.val();

        if(this.expression == $val)
            return false;

        this.expression = this.input.val();

        return true;

    },

    request: function(){

        calculator.warnig.hide();
        calculator.loading.show();

        parameters = {'value' : calculator.expression};

        $.ajax({
            type: "POST",
            url: calculator.wsdl,
            data: parameters,
            dataType: "xml",
            success: function(response) {
                calculator.loading.hide();
                calculator.output.text('= ' + response.childNodes[0].textContent);
            },
            error: function(response, status, error){
                calculator.loading.hide();
                calculator.warnig.show().text("web service " + status);
                //alert(JSON.stringify(response));
            }
        });

    }
}


$(function() {

 calculator.input ='#expression';
 calculator.output ='#result';
 calculator.warnig ='#warnig';
 calculator.loading ='#loading';
 calculator.wsdl ='#wsdl';

 calculator.ready();

});